<?php
namespace App\Library\Orders;

//namespace App\Http\Controllers\Api;
use App\Models\Currency;
use App\Models\Entrust;
use App\Models\Property;
use App\Models\PropertyInfo;
use App\Models\Order;
use App\Models\Message;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Cloud;
use Exception;
use DB;

/**
 * Class Order
 * @package App\Library\Order1111
 */
class Orders
{
    public function cancel($id){
        $list = array();
        $infos = Entrust::where('id', $id)->whereIn('status', [1, 2])->first();
        if ($infos) {
            $currency = $infos->currency->buy_fee;
            if (!empty((float) $infos->deal)) {
                //部分取消
                $data['status'] = 4;
            } else {
                //全部取消
                $data['status'] = 5;
            }
            if ($infos->type == 3) {
                $money              = bcmul($infos->num - $infos->deal, $infos->price, 8) + bcmul($infos->num - $infos->deal, bcmul($infos->price / 100, $currency, 8), 8);
                $where['uid']       = $infos->uid;
                $map['is_standard'] = 1;
                $map['pay_switch']  = 1;
                $map['buy_switch']  = 1;
                $where['cid']       = Currency::where($map)->first(['id'])->id;
            } else {
                $money        = $infos->num - $infos->deal;
                $where['uid'] = $infos->uid;
                $where['cid'] = $infos->cid;
            }
            try {
                //开启事务
                DB::beginTransaction();
                $infos->update($data);
                Property::where($where)->increment('balance', $money);
                Property::where($where)->decrement('blocked', $money);
                DB::commit();
                $list['status']= 1;
                $list['uid']= $infos->uid;
                $list['number_no']= $infos->number_no;
                return $list;
            } catch (Exception $e) {
                DB::rollback();
                $list['status']= 2;
                return $list;
            }
        }
        $list['status']= 0;
        return $list;
    }
    /**
     * 获取货币最新交易价格
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/18
     * @access public
     * @return string
     */
    public function currency_price($cid){
        $where['cid'] = $cid;
        $infos = order::where($where)->orderBy('id','desc')->first();
        if (!$infos){
            $curr = $this->currency_verify($cid);
            if (!$curr)
                return false;
            $price = $curr->official_price;
        }else{
          $price = $infos->price;
        }
        return $price;
    }
    /**
     * 查询资产 没有则自动生成资产 或更改资产显示状态
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/17
     * @access public|private|protected
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function property_verif($uid,$cid){
        $where['cid'] = $cid;
        $where['uid'] = $uid;
        $infos = Property::where($where)->first();
        if (!$infos){
            $data['cid'] = $cid;
            $data['uid'] = $uid;
            $data['status'] = 2;
            $id = Property::create($data);
            $infos = Property::where($data)->first();

        }
        return $infos;
    }

    /**
     * 验证货币方法
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/13
     * @access public
     * @param  int    id    货币主键ID
     * @return json
     */
    public function currency_verify($id)
    {
        $where['id'] = $id;
        $where['pay_switch'] = 1;
        $where['buy_switch'] = 1;

        $infos = Currency::where($where)->first();

        if (!$infos) {
            return false;
        }
        return $infos;
    }
    /**
     * 获取所有在线货币ID
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/18
     * @access public
     * @return array
     */
    public function currency_id()
    {
        $where['pay_switch'] = 1;
        $where['buy_switch'] = 1;
        $infos = Currency::where($where)->orderBy('orderby','asc')->pluck('id');
        if (!$infos) {
            return false;
        }
        return $infos;
    }
    /**
     * 获取本位币ID
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/13
     * @access public|private|protected
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function get_standard(){
        $map['is_standard'] = 1;
        $map['pay_switch'] = 1;
        $map['buy_switch'] = 1;
        $currency_info = Currency::where($map)->first(['id']);
        if (!$currency_info){
            return respF(1000);
        }
        return $currency_info->id;
    }

    /**
     * 匹配订单
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/23
     * @access public
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function pair_deal($id)
    {
        if (empty($id))
            return false;
        $list = Entrust::where('id',$id)->first();
        if (!$list)
            return false;
        $c_info = $this->currency_verify($list->cid);//交易货币的信息
        $b_c_id = $this->get_standard();
        $where_p['cid'] = $list->cid;
        $msg_model = new Message();
        if ($list->type==3) {  //买入
            $where_p['type'] = 4;
            $list_a = Entrust::where($where_p)->where('price','<=',(float)$list->price)->whereIn('status',[1,2])->first();
            if ($list_a) {
                do{
                    //匹配卖的订单
                    $info_a = Entrust::where($where_p)->where('price','<=',(float)$list->price)->whereIn('status',[1,2])->orderBy('price','asc')->first();//卖的数据
                    $info_b = Entrust::where('id',$id)->whereIn('status',[1,2])->first();//买家数据
                    if ($info_a &&$info_b){
                        $num1 = $info_a->num - $info_a->deal;//卖订单剩余量
                        $num2 = $info_b->num - $info_b->deal;//买订单剩余量
                        if ($num1<=$num2){
                            $num3 = $num1;//当前交易数量
                        }else{
                            $num3 = $num2;//当前交易数量
                        }
                        //修改卖方委托单数据
                        $data1['deal'] = $num3+$info_a->deal;
                        $data1['deal_time'] = time();
                        if ($info_a->num == $data1['deal']){
                            $data1['status'] =3;//订单完成
                        }else{
                            $data1['status'] =2;
                        }
                        //修改买方委托单数据
                        $data2['deal'] = $num3+$info_b->deal;
                        $data2['deal_time'] = time();
                        if ($data2['deal']==$info_b->num){
                            $data2['status'] = 3;
                        }else{
                            $data2['status'] = 2;
                        }
                        //修改资产数据
                        //卖方 扣除卖出的货币  得到本位币（价格x数量-手续费）
                        $s_proc = $num3*$info_a->price*($c_info->fee)/100;//卖方手续费
                        $u_proc = ($info_a->price)*($c_info->fee)*$num3/100;//买方扣除USDT手续费
                        $uu_proc = ($info_b->price)*($c_info->fee)*$num3/100;//买方已冻结USDT手续费
                        //卖方资产信息变更
                        //交易币减少  扣除冻结的
                        $where_s1['uid'] = $info_a->uid;
                        $where_s1['cid'] = $info_a->cid;
                        $money_s1 = $num3;
                        //本位币增加
                        $where_s2['uid'] = $info_a->uid;
                        $where_s2['cid'] = $b_c_id;
                        $money_s2 = bcmul($info_a->price, $num3, 8)-$s_proc;
                        //买方资产信息变更
                        //交易币增加
                        $where_u1['uid'] = $info_b->uid;
                        $where_u1['cid'] = $info_b->cid;
                        $money_u1 = $num3;
                        //本位币减少
                        $where_u2['uid'] = $info_b->uid;
                        $where_u2['cid'] = $b_c_id;
                        $money_u2 = bcmul($info_a->price, $num3, 8)+$u_proc;
                        //买方
                        try {

                            //开启事务
                            DB::beginTransaction();
                            //检查用户资产信息
                            $user_p = $this->check_balance($info_b->uid,$info_b->cid);
                            if (!$user_p)
                                return FALSE;

                            //卖的委托单信息更改
                            $info_a->update($data1);

                            //买的委托单信息更改
                            $info_b->update($data2);
                            //var_dump($where_s1);
                            //var_dump($money_s1);
                            //$b = Property::where($where_s1)->first()->toArray();


                            Property::where($where_s1)->decrement('blocked',$money_s1);
                            Property::where($where_s1)->decrement('total',$money_s1);
                            Property::where($where_s2)->increment('balance',$money_s2);
                            Property::where($where_s2)->increment('total',$money_s2);

                            Property::where($where_u1)->increment('balance',$money_u1);
                            Property::where($where_u1)->increment('total',$money_u1);

                            if ((float)$list->price>(float)$info_a->price){
                                $money_cj = bcmul($list->price-$info_a->price, $num3, 8)-$u_proc+$uu_proc;
                                Property::where($where_u2)->decrement('blocked',$money_u2+$money_cj);
                                Property::where($where_u2)->increment('balance',$money_cj);
                                Property::where($where_u2)->decrement('total',$money_u2);
                            }else{
                                Property::where($where_u2)->decrement('blocked',$money_u2);
                                Property::where($where_u2)->decrement('total',$money_u2);
                            }
                            //生成资产记录和订单信息
                            $re = $this->place_order($id,$info_a->id,$info_b->uid,$info_a->uid,$info_a->cid,$info_a->price,$num3,$u_proc,$s_proc,$info_b->number_no,$info_a->number_no);
                            if ($re!=true)
                                return false;
                            DB::commit();
                            //生成通知消息
                            if ($num3==$list->num){
                                $msg = [
                                    'msg_id'=>5,
                                    'uid'=>$info_b->uid,
                                    'message'=>[
                                        date('Y-m-d H:i:s', $data2['deal_time']),//时间
                                        $c_info->name_en,//货币名称
                                        $list->num,//委托数量price
                                        $list->price,//委托单价
                                        $num3//成交数量
                                    ],
                                    'push_message'=>[
                                        $c_info->name_en//货币名称
                                    ],
                                    'title'=>[
                                        $c_info->name_en
                                    ]
                                ]; //全部成交
                            }else{
                                $msg = [
                                    'msg_id'=>7,
                                    'uid'=>$info_b->uid,
                                    'message'=>[
                                        date('Y-m-d H:i:s', $data2['deal_time']),//时间
                                        $c_info->name_en,//货币名称
                                        $list->num,//委托数量price
                                        $list->price,//委托单价
                                        $num3,//成交数量
                                        $info_a->price//成交单价
                                    ],
                                    'push_message'=>[
                                        $c_info->name_en//货币名称
                                    ],
                                    'title'=>[
                                        $c_info->name_en
                                    ]
                                ]; //全部成交
                            }

                            if ($num3==$info_a->num){
                                $msg1 = [
                                    'msg_id'=>6,
                                    'uid'=>$info_a->uid,
                                    'message'=>[
                                        date('Y-m-d H:i:s', $data2['deal_time']),//时间
                                        $c_info->name_en,//货币名称
                                        $list->num,//委托数量price
                                        $list->price,//委托单价
                                        $num3//成交数量
                                    ],
                                    'push_message'=>[
                                        $c_info->name_en//货币名称
                                    ],
                                    'title'=>[
                                        $c_info->name_en
                                    ]
                                ]; //全部成交
                            }else{
                                $msg1 = [
                                    'msg_id'=>8,
                                    'uid'=>$info_a->uid,
                                    'message'=>[
                                        date('Y-m-d H:i:s', $data2['deal_time']),//时间
                                        $c_info->name_en,//货币名称
                                        $list->num,//委托数量price
                                        $list->price,//委托单价
                                        $num3,//成交数量
                                        $info_a->price//成交单价
                                    ],
                                    'push_message'=>[
                                        $c_info->name_en//货币名称
                                    ],
                                    'title'=>[
                                        $c_info->name_en
                                    ]
                                ]; //全部成交
                            }
                            $msg_model->syspublishsmsAll($msg);
                            $msg_model->syspublishsmsAll($msg1);
                        } catch (Exception $e) {
                            DB::rollback();
                            return false;
                        }
                    }
                }while(Entrust::where('id',$id)->first()->status==2&&(!empty(Entrust::where($where_p)->where('price','<=',(float)$list->price)->whereIn('status',[1,2])->first())));
            }
            return true;
        } else {//卖委托单   匹配买
            $where_p['type'] = 3;
            $list_a = Entrust::where($where_p)->where('price','>=',(float)$list->price)->whereIn('status',[1,2])->first();

            if ($list_a) {
                do{
                    //匹配买的订单
                    $info_a = Entrust::where('id',$id)->whereIn('status',[1,2])->first();//卖家数据
                    $info_b = Entrust::where($where_p)->where('price','>=',(float)$list->price)->whereIn('status',[1,2])->orderBy('price','desc')->first();//买的数据
                    if ($info_a &&$info_b){
                        $num1 = $info_a->num - $info_a->deal;
                        $num2 = $info_b->num - $info_b->deal;
                        if ($num1<=$num2){
                            $num3 = $num1;
                        }else{
                            $num3 = $num2;
                        }
                        //修改卖方委托单数据
                        $data1['deal'] = $num3+$info_a->deal;
                        $data1['deal_time'] = time();

                        if ($info_a->num == $data1['deal']){
                            $data1['status'] =3;
                        }else{
                            $data1['status'] =2;
                        }
                        //修改买方委托单数据
                        $data2['deal'] = $num3+$info_b->deal;
                        $data2['deal_time'] = time();

                        if ($data2['deal']==$info_b->num){
                            $data2['status'] = 3;
                        }else{
                            $data2['status'] = 2;
                        }
                        //修改资产数据
                        //卖方 扣除卖出的货币  得到本位币（价格x数量-手续费）
                        $s_proc = $num3*$info_b->price*($c_info->fee)/100;//卖方扣除得到本位币的手续费
                        $u_proc = $info_b->price*$c_info->fee*$num3/100;//买房扣除交易货币的手续费
                        //卖方资产信息变更
                        //交易币减少  扣除冻结的
                        $where_s1['uid'] = $info_a->uid;
                        $where_s1['cid'] = $info_a->cid;
                        $money_s1 = $num3;
                        //本位币增加
                        $where_s2['uid'] = $info_a->uid;
                        $where_s2['cid'] = $b_c_id;
                        $money_s2 = $info_b->price*$num3-$s_proc;
                        //买方资产信息变更
                        //交易币增加
                        $where_u1['uid'] = $info_b->uid;
                        $where_u1['cid'] = $info_b->cid;
                        $money_u1 = $num3;
                        //本位币减少
                        $where_u2['uid'] = $info_b->uid;
                        $where_u2['cid'] = $b_c_id;
                        $money_u2 = $info_b->price*$num3+$u_proc;

                        //买方
                        try {
                            //开启事务
                            DB::beginTransaction();
                            //检查用户资产信息
                            $user_p = $this->check_balance($info_b->uid,$info_b->cid);
                            if (!$user_p)
                                return false;
                            //卖的委托单信息更改
                            $info_a->update($data1);
                            //买的委托单信息更改
                            $info_b->update($data2);
                            Property::where($where_s2)->increment('balance',$money_s2);
                            Property::where($where_s2)->increment('total',$money_s2);
                            Property::where($where_u2)->decrement('blocked',$money_u2);
                            Property::where($where_u2)->decrement('total',$money_u2);
                            Property::where($where_u1)->increment('balance',$money_u1);
                            Property::where($where_u1)->increment('total',$money_u1);
                            Property::where($where_s1)->decrement('blocked',$money_s1);
                            Property::where($where_s1)->decrement('total',$money_s1);


                            //生成资产记录和订单信息
                            $re = $this->place_order($id,$info_b->id,$info_b->uid,$info_a->uid,$info_a->cid,$info_b->price,$num3,$u_proc,$s_proc,$info_b->number_no,$info_a->number_no);
                            if ($re!=true)
                                return false;
                            DB::commit();
                            //生成通知消息
                            if ($num3==$info_a->num){
                                $msg = [
                                    'msg_id'=>6,
                                    'uid'=>$info_a->uid,
                                    'message'=>[
                                        date('Y-m-d H:i:s', $data1['deal_time']),//时间
                                        $c_info->name_en,//货币名称
                                        $info_b->num,//委托数量price
                                        $info_b->price,//委托单价
                                        $num3//成交数量
                                    ],
                                    'push_message'=>[
                                        $c_info->name_en//货币名称
                                    ],
                                    'title'=>[
                                        $c_info->name_en
                                    ]
                                ]; //全部成交
                            }else{
                                $msg = [
                                    'msg_id'=>8,
                                    'uid'=>$info_a->uid,
                                    'message'=>[
                                        date('Y-m-d H:i:s', $data1['deal_time']),//时间
                                        $c_info->name_en,//货币名称
                                        $info_b->num,//委托数量price
                                        $info_b->price,//委托单价
                                        $num3,//成交数量
                                        $info_b->price//成交单价
                                    ],
                                    'push_message'=>[
                                        $c_info->name_en//货币名称
                                    ],
                                    'title'=>[
                                        $c_info->name_en
                                    ]
                                ]; //全部成交
                            }


                            if ($num3==$info_b->num){
                                $msg1 = [
                                    'msg_id'=>5,
                                    'uid'=>$info_b->uid,
                                    'message'=>[
                                        date('Y-m-d H:i:s', $data2['deal_time']),//时间
                                        $c_info->name_en,//货币名称
                                        $info_b->num,//委托数量price
                                        $info_b->price,//委托单价
                                        $num3//成交数量
                                    ],
                                    'push_message'=>[
                                        $c_info->name_en//货币名称
                                    ],
                                    'title'=>[
                                        $c_info->name_en
                                    ]
                                ]; //全部成交
                            }else{
                                $msg1 = [
                                    'msg_id'=>7,
                                    'uid'=>$info_b->uid,
                                    'message'=>[
                                        date('Y-m-d H:i:s', $data2['deal_time']),//时间
                                        $c_info->name_en,//货币名称
                                        $info_b->num,//委托数量price
                                        $info_b->price,//委托单价
                                        $num3,//成交数量
                                        $info_a->price//成交单价
                                    ],
                                    'push_message'=>[
                                        $c_info->name_en//货币名称
                                    ],
                                    'title'=>[
                                        $c_info->name_en
                                    ]
                                ]; //全部成交
                            }
                            $msg_model->syspublishsmsAll($msg);
                            $msg_model->syspublishsmsAll($msg1);
                        } catch (Exception $e) {
                            DB::rollback();
                            return false;
                        }
                    }
                }while(Entrust::where('id',$id)->first()->status==2&&(!empty(Entrust::where($where_p)->where('price','>=',(float)$list->price)->whereIn('status',[1,2])->first())));
            }
            return true;
        }
    }


    /**
     * 下订单
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/19
     * @access public|private|protected
     * @param  int    eid    委托单ID
     * @param  int    uid    买家ID
     * @param  int    sid    卖家ID
     * @param  int    cid    货币ID
     * @param  int    type    交易类型  1买  2卖
     * @param  string    price    成交价格
     * @param  string    u_proc    买方手续费
     * @param  string    s_proc    卖方手续费
     * @param  string    real_price    实际价 = 总价-手续费
     * @param  string   total_price    总价
     * @param  string   num    数量
     * @return boolean      成功/失败
     */
    public function place_order($u_eid='', $s_eid='', $uid='', $sid='', $cid='', $price='', $num='', $u_proc='', $s_proc='', $order_no_a='', $order_no_b='')
    {
        if (empty($u_eid)||empty($s_eid)||empty($uid)||empty($sid)||empty($cid)||empty($price)||empty($u_proc)||empty($s_proc)||empty($num)||empty($order_no_a)||empty($order_no_b)) {
            return false;
        }

        $total_price = $num*$price;
        //买房方订单数数据
        $data_a['eid'] = $u_eid;
        $data_a['uid'] = $uid;
        $data_a['sid'] = $sid;
        $data_a['cid'] = $cid;
        $data_a['price'] = $price;
        $data_a['num'] = $num;
        $data_a['proc'] = $u_proc;
        $data_a['total_price'] = $total_price;
        $data_a['type'] = 1;
        $data_a['order_no'] = get_microtimes('JYD');
        $data_a['number_no'] = $order_no_a;
        $data_a['real_price'] = $total_price-$u_proc;
        $data_a['created_at'] = date('Y-m-d H:i:s');
        $data_a['updated_at'] = date('Y-m-d H:i:s');
        //卖方订单数据
        $data_b['eid'] = $s_eid;
        $data_b['uid'] = $sid;
        $data_b['sid'] = $uid;
        $data_b['cid'] = $cid;
        $data_b['price'] = $price;
        $data_b['num'] = $num;
        $data_b['proc'] = $s_proc;
        $data_b['total_price'] = $total_price;
        $data_b['type'] = 2;
        $data_b['order_no'] = get_microtimes('JYD');
        $data_b['number_no'] = $order_no_b;
        $data_b['real_price'] = $total_price-$s_proc;
        $data_b['created_at'] = date('Y-m-d H:i:s');
        $data_b['updated_at'] = date('Y-m-d H:i:s');




        //资金记录--买记录获得
        $data1['uid'] = $uid;
        $data1['cid'] = $cid;
        $data1['note'] = '委托买入获得';
        $data1['num'] = $num;
        $data1['price'] = $price;
        $data1['total'] = $total_price;
        $data1['proc'] = $u_proc;
        $data1['type'] = 3;
        $data1['deal_type'] = 1;
        $data1['status'] = 1;
        $data1['created_at'] = date('Y-m-d H:i:s');
        $data1['updated_at'] = date('Y-m-d H:i:s');
        //资金记录--买记录付款
        $data5['uid'] = $uid;
        $data5['cid'] = $this->get_standard();
        $data5['note'] = '委托买入付款';
        $data5['num'] = $num;
        $data5['price'] = $price;
        $data5['total'] = $total_price;
        $data5['proc'] = $u_proc;
        $data5['type'] = 3;
        $data5['deal_type'] = 2;
        $data5['status'] = 1;
        $data5['created_at'] = date('Y-m-d H:i:s');
        $data5['updated_at'] = date('Y-m-d H:i:s');

        //资金记录--买方手续费
        $data2['uid'] = $uid;
        $data2['cid'] = $this->get_standard();
        $data2['note'] = '委托买入手续费';
        $data2['num'] = $num;
        $data2['price'] = $price;
        $data2['total'] = $total_price;
        $data2['proc'] = $u_proc;
        $data2['type'] = 3;
        $data2['deal_type'] = 3;
        $data2['status'] = 1;
        $data2['created_at'] = date('Y-m-d H:i:s');
        $data2['updated_at'] = date('Y-m-d H:i:s');





        //资金记录--卖记录获得
        $data3['uid'] = $sid;
        $data3['cid'] = $this->get_standard();
        $data3['note'] = '委托卖出获得';
        $data3['num'] = $num;
        $data3['price'] = $price;
        $data3['total'] = $total_price-$s_proc;
        $data3['proc'] = $s_proc;
        $data3['type'] = 4;
        $data3['deal_type'] = 1;
        $data3['status'] = 1;
        $data2['created_at'] = date('Y-m-d H:i:s');
        $data2['updated_at'] = date('Y-m-d H:i:s');
        //资金记录--卖方付款
        $data6['uid'] = $sid;
        $data6['cid'] = $cid;
        $data6['note'] = '委托卖出付款';
        $data6['num'] = $num;
        $data6['price'] = $price;
        $data6['total'] = $total_price;
        $data6['proc'] = $s_proc;
        $data6['type'] = 4;
        $data6['deal_type'] = 2;
        $data6['status'] = 1;
        $data6['created_at'] = date('Y-m-d H:i:s');
        $data6['updated_at'] = date('Y-m-d H:i:s');
        //资金记录--卖方手续费
        $data4['uid'] = $sid;
        $data4['cid'] = $this->get_standard();
        $data4['note'] = '委托卖出手续费';
        $data4['num'] = $num;
        $data4['price'] = $price;
        $data4['total'] = $total_price;
        $data4['proc'] = $s_proc;
        $data4['type'] = 4;
        $data4['deal_type'] = 3;
        $data4['status'] = 1;
        $data4['created_at'] = date('Y-m-d H:i:s');
        $data4['updated_at'] = date('Y-m-d H:i:s');

        try {
            //开启事务
            DB::beginTransaction();
            $order_id_a = Order::insertGetId($data_a);
            if ($order_id_a) {
                $data1['oid'] = $order_id_a;
                $data2['oid'] = $order_id_a;
                $data5['oid'] = $order_id_a;
            }
            $order_id_b = Order::insertGetId($data_b);
            if ($order_id_b) {
                $data3['oid'] = $order_id_b;
                $data4['oid'] = $order_id_b;
                $data6['oid'] = $order_id_b;
            }
            PropertyInfo::insert($data1);
            PropertyInfo::insert($data2);
            PropertyInfo::insert($data3);
            PropertyInfo::insert($data4);
            PropertyInfo::insert($data5);
            PropertyInfo::insert($data6);
            DB::commit();
            return true;
        } catch (Exception $e) {
            DB::rollback();
            return false;
        }
    }


    /**
     * 检查资产信息
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/23
     * @access public
     * @param  int    $uid    用户ID
     * @param  int    $cid    货币ID
     * @return boolean
     */
    public function check_balance($uid, $cid)
    {
        $where['uid'] = $uid;
        $where['cid'] = $cid;
        $infos = Property::where($where)->first();
        if (empty($infos)) {
            $re = Property::insert($where);
            if ($re) {
                return true;
            }
            return false;
        } else {
            return true;
        }
    }


    public function s_e($cid){
        $list = array();
        $list['cid'] = $cid;
        //dump($cid);
        $currency_info = $this->currency_verify($cid);
        //买入数据
        $where['cid'] = $cid;
        $where['type']  = 3;
        $infos = Entrust::where($where)->whereIn('status',[1,2])->orderBy('price','desc')->take(5)->get(['price','deal','num']);
        //无数据时
        $arr = [
            '0'=>['price'=>0,'y_num'=>0],
            '1'=>['price'=>0,'y_num'=>0],
            '2'=>['price'=>0,'y_num'=>0],
            '3'=>['price'=>0,'y_num'=>0],
            '4'=>['price'=>0,'y_num'=>0]
        ];
        $arr1 = [
            '0'=>['price'=>0,'y_num'=>0],
            '1'=>['price'=>0,'y_num'=>0],
            '2'=>['price'=>0,'y_num'=>0],
            '3'=>['price'=>0,'y_num'=>0],
            '4'=>['price'=>0,'y_num'=>0]
        ];
        if (count($infos)<1){
            $list['buy'] = $arr;
        }else{
            foreach($infos as $k =>$v){
                $list['buy'][$k]['price'] = number_format($v->price, 2);
                $list['buy'][$k]['y_num'] = number_format($v->num-$v->deal, 4);
            }
            switch (count($list['buy'])){
                case 1:
                    unset($arr[0]);
                    $list['buy'] = array_merge($list['buy'],$arr);
                    break;
                case 2:
                    unset($arr[0]);
                    unset($arr[1]);
                    $list['buy'] = array_merge($list['buy'],$arr);
                    break;
                case 3:
                    unset($arr[0]);
                    unset($arr[1]);
                    unset($arr[2]);
                    $list['buy'] = array_merge($list['buy'],$arr);
                    break;
                case 4:
                    unset($arr[0]);
                    unset($arr[1]);
                    unset($arr[2]);
                    unset($arr[3]);
                    $list['buy'] = array_merge($list['buy'],$arr);
                    break;
                case 5:
                    break;
            }
        }
        //卖出数据
        $where1['cid'] = $cid;
        $where1['type']  = 4;
        $infos3 = Entrust::where($where1)->whereIn('status',[1,2])->orderBy('price','asc')->take(5)->get(['price','deal','num']);
        if (count($infos3)<1){
            $list['sell'] = $arr1;
        }else{
            foreach($infos3 as $k =>$v){
                $list['sell'][$k]['price'] = number_format($v->price, 2);
                $list['sell'][$k]['y_num'] = number_format($v->num-$v->deal, 4);
            }
            $list['sell'] = array_reverse($list['sell']);

            switch (count($list['sell'])){
                case 1:
                    unset($arr1[0]);
                    $list['sell'] = array_merge($list['sell'],$arr1);
                    break;
                case 2:
                    unset($arr1[0]);
                    unset($arr1[1]);
                    $list['sell'] = array_merge($list['sell'],$arr1);
                    break;
                case 3:
                    unset($arr1[0]);
                    unset($arr1[1]);
                    unset($arr1[2]);
                    $list['sell'] = array_merge($list['sell'],$arr1);
                    break;
                case 4:
                    unset($arr1[0]);
                    unset($arr1[1]);
                    unset($arr1[2]);
                    unset($arr1[3]);
                    $list['sell'] = array_merge($list['sell'],$arr1);
                    break;
                case 5:
                    break;
            }
        }
        $type    = 'publish';
        $content = json_encode($list);
        $uid = 'c_m';
        $push_api_url = "http://114.215.40.82:2121/";
        //$push_api_url = "http://sj1.51tuanli.com/";
        $post_data = [
            "type" => $type,
            "content" => $content,
            "to" => $uid,
        ];

//dump($post_data);die;
        $ch = curl_init ();

        curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
        curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
        //dd($ch);
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        //dump($return);
    }
}
